/*  This file is part of the comicconquer project
    Copyright (C) 2010 Reach <reach334@gmail.com>

    ComicConquer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This software is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public 
    License along with this software.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "BookmarkDlg.h"
#include "onyx/ui/message_dialog.h"

extern QDialog* g_mainDialog;

/**
 * Constructor.
 * 
 * @author reach (5/25/2010)
 * 
 * @param profile: The profile.
 * @param isRemove: Whether this dialog is used to remove or open.
 * @param parent: Parent widget.
 */
BookmarkDlg::BookmarkDlg(CoconProfile* profile, bool isRemove, QWidget* parent)
: ui::OnyxDialog(parent)
{
    In_Log(__FUNCTION__, COCLASSNAME);

    if (isRemove)
    {
        updateTitle(tr("删除书签"));
    }
    else
    {
        updateTitle(tr("进入书签"));
    }

    m_profile = profile;
    m_isRemove = isRemove;

    content_widget_.setLayout(&m_layout);

    m_list = new CoListWidget(NULL, this);

    m_layout.addWidget(m_list);
    connect(m_list, SIGNAL(activated(const QModelIndex&)), this, SLOT(selectedItem(const QModelIndex&)));

    connect(this, SIGNAL(requestOpenComic(const QString&)), g_mainDialog, SLOT(openComic(const QString&)));

    initList();

    Out_Log(__FUNCTION__, COCLASSNAME);
}

/**
 * Initialize CoListWidget.
 * 
 * @author reach (5/25/2010)
 */
void BookmarkDlg::initList()
{
    Bookmarks& bookmarks = m_profile->bookmarks;

    m_listItems.clear();

    for (int i = 0; i < bookmarks.count(); i++)
    {
        QString str = bookmarks[i].path;
        int index = str.indexOf('/', 2);
        str.remove(0, index);
        m_listItems.append(CoListItem(str + " <" + bookmarks[i].date + ">", i));
    }

    m_list->setListItems(&m_listItems);
    update();
}

/**
 * Static interface to run the dialog.
 * 
 * @author reach (5/25/2010)
 * 
 * @param profile: The profile.
 * @param isRemove: Whether this dialog is used to remove or open.
 * @param parent: Parent widget.
 */
void BookmarkDlg::manageBookmark(CoconProfile* profile, bool isRemove, QWidget* parent)
{
    BookmarkDlg dlg(profile, isRemove, parent);

#ifdef QT_ARCH_ARM
    dlg.showFullScreen();
#else
    if (parent != 0)
    {
        dlg.setFixedSize(parent->size());
    }
    else
    {
        dlg.setFixedSize(600, 800);
    }
#endif

    dlg.exec();
}

/**
 * The slot connect to CoListWidget.
 * 
 * @author reach (5/25/2010)
 * 
 * @param index: The QModeIndex of selected item in CoListWidget.
 */
void BookmarkDlg::selectedItem(const QModelIndex& index)
{
    int id = index.row();

    if (!m_isRemove)
    {
        accept();
        emit requestOpenComic(m_profile->bookmarks[id].path);
    }
    else
    {
        ui::MessageDialog removeDlg(QMessageBox::Question,
                                    tr("询问"),
                                    tr("删除书签:\"") + m_profile->bookmarks[id].path + tr("\"？"),
                                    QMessageBox::Yes | QMessageBox::No);
        int ret = removeDlg.exec();

        if (ret == QMessageBox::Yes)
        {
            m_profile->removeBookmark(id);
            initList();
        }
        else
        {
            update();
        }
    }
}

/**
 * Handle update request event.
 * 
 * @author reach (5/25/2010)
 * 
 * @param e: The event from QApplication.
 * 
 * @return bool 
 */
bool BookmarkDlg::event(QEvent* e)
{
    bool ret = ui::OnyxDialog::event(e);

    switch (e->type())
    {
    case QEvent::UpdateRequest:
        {
            refreshScreen();
            return true;
        }
        break;
    default:
        {

        }
        break;
    }
    return ret;
}

/**
 * Refresh screen of OnyxBoox.
 * 
 * @author reach (5/25/2010)
 * 
 * @param w: Flush waveform.
 * @param whole: Is flush the whole screen or not.
 */
void BookmarkDlg::refreshScreen(ScreenProxy::Waveform w, bool whole)
{
    ScreenProxy::instance().updateWidget(this, w, whole);
}

/**
 * Let the CoListWidget to handle key release event.
 * 
 * @author reach (5/25/2010)
 * 
 * @param event: The key event from QApplication.
 */
void BookmarkDlg::keyReleaseEvent(QKeyEvent* event)
{
    m_list->keyReleaseEvent(event);
}

/**
 * Exit when the "Back" key is pressed.
 * 
 * @author reach (5/25/2010)
 * 
 * @param event: The key event from QApplication.
 */
void BookmarkDlg::keyPressEvent(QKeyEvent* event)
{
    if (event->key() == Qt::Key_Escape)
    {
        reject();
    }
}

